#include<stdio.h>
#include<stdlib.h>
int i=-1;
int *asd;

void append(int n)
{
	i++;
	asd=realloc(asd,(i+1)*sizeof(int*));
	asd[i]=n;
	
}


void extend(int n,int b[100])
{
	int y;
	asd=realloc(asd,(n+i)*sizeof(int*));	
		for(y=0;y<n;y++)
		{ 
		i++;
		asd[i]=b[y];
		}
}


void insert(int n,int m)
{
	asd=realloc(asd,(i+1)*sizeof(int*));
	int k,temp,temp1;
	temp=asd[m-1];
	asd[m-1]=n;
	for(k=i;k>=m;k--)
		{	
			asd[k+1]=asd[k];
			
		}
	asd[m]=temp;
i++;
}




void remove1(int n)
{
	asd=realloc(asd,(i-1)*sizeof(int*));
	int l,flag=0,c;
	for(l=0;l<=i;l++)
		{
			if(asd[l]==n)
			{
				
				flag=1;
				for(c=l;c<=i;c++)
				{
					asd[c]=asd[c+1];
					
				}
			i--;
			break;	
			}

		}
printf(" %d",i);	

	printf(" %d",i);
	if(flag==0)
	printf("element not found");
		
}

int pop(int r)
{
int temp,b;
	asd=realloc(asd,(i-1)*sizeof(int*));
	temp=asd[r-1];
	for(b=r;b<i;b++)
	{
		asd[b]=asd[b-1];
		break;
		
	}

	i--;
	return temp;
}


int index1(int n)
{
int flag=0,g;
for(g=0;g<=i;g++)
	{
	
		if(asd[g]==n)
		{
			flag=1;
			return g+1;
		}
	}
	if(flag==0)
	printf("element not found");
}
	
	

int count(int n)
{
int count1=0,flag=0,g;
	for(g=0;g<=i;g++)
	{
		flag=1;
		if(asd[g]==n)
		{
			count1++;
		}
	}
	if(flag==0)
	printf("element not found");
		
return count1;
}


void sort(int r)
{

int o,p,temp;
	if(r==1)
	{
		for(o=0;o<=i;o++)
		{
			for(p=0;p<=i;p++)
			{
				if(asd[o]>asd[p])
				{
					temp=asd[o];
					asd[o]=asd[p];
					asd[p]=temp;
				}
			}
		}
	}
	else
	{
	for(o=0;o<=i;o++)
		{
			for(p=0;p<=i;p++)
			{
				if(asd[o]<asd[p])
				{
					temp=asd[o];
					asd[o]=asd[p];
					asd[p]=temp;
				}
			}
		}
	}
}








void reverse()
{

int t,temp;
int y=i;

	for(t=0;t<(i/2);t++)
	{
		temp=asd[t];
		asd[t]=asd[y];
		asd[y]=temp;
		y--;
	
	}
}


void display()
{
int t;
for (t=0;t<=i;t++)
{
printf("Element at position %d is %d \n",(t+1),asd[t]);
}
}


void main()
{
int w;
int c,v[100],r;
int num;
int g,h;
int d1,x;
while(1)  
{
    printf("\n1.To append the list \n2.To extend the list\n3.To insert in list \n4.To remove from a list\n5.To pop a element from list \n6.To return the index1 of number in the list\n7.To count the number of occurance of a number in list \n8.To reverse a list \n9.To sort the list \n10.To display the list \n0.To exit\n");
    scanf(" %d",&w);
 
    switch(w)
    {
        case 1:
         {

		printf("Enter the number u want to append \n");
		scanf(" %d",&num);
		append(num);
		break;
		
	 }
	case 2:
          {
		
		printf("Enter the list of number u want to append \n");
		scanf(" %d",&c);
				
		for(r=0;r<c;r++)
		scanf(" %d",&v[r]);
		extend(c,v);
	
		break;
          }
	case 3:
	 {
		
		printf("Enter the number u want to insert \n");
		scanf(" %d",&h);
		printf("Enter the position at which u want to insert \n");
		scanf(" %d",&g);
		if(g>i+2){
		printf("ERROR Index does not Exist");
		
		}		
		else
		insert(h,g);
				
		break;

	 }
	case 4:
         {
		
		printf("Enter the number u want to delete \n");
		scanf(" %d",&d1);
		remove1(d1);
		break;
	 }
	case 5:
          {
		
		printf("Enter the position at which u want to delete(Enter -1 if u do want to specify the position) \n");
		scanf(" %d",&h);
		if(h<=i+1)
		{
		if(h==-1)
			x=pop(i);
		else
			x=pop(h);
		printf("Element being deleted is %d",x);
		}
		else
		printf("ERROR Index does not Exist");
		break;
	          }
	case 6:
	 {
		
		printf("Enter the element whose index1 u want to find \n");
		scanf(" %d",&h);
		x=index1(h);
		printf("Element index1 is %d  \n",x);
		break;	
	}
	case 7:
         {
		
		printf("Enter the element whose count u want to find \n");
		scanf(" %d",&h);
		x=count(h);
		printf("Element count is %d \n",x);
		break;
	 }
	case 8:
          {
		reverse();
		break;
          }
	case 9:
	 {
		int m;
		printf("enter 0 for acending order and 1 for descending \n");
		scanf(" %d",&m);
		if(m==0)
		sort(0);
		else
		sort(1);
		break;
	 }
	case 10:
	{
	display();
	break;
	}
	
	case 0:
	 {
	  exit(0);
	 }
	default:
	{
         printf("wrong choice entered");
	}
}
}
}
